﻿using System;
using System.Web.UI;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

 
namespace mojoPortal.Web.RS.SBM
{
    public partial class RSWorkOrderEquips : RSBasePage
    {
        int _companyid = 0;
        int _workorderid = 0;
        int _locationid = 0;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage("WorkOrder Detail");
                if (Request.QueryString["workorderid"] != null)
                    _workorderid = Convert.ToInt32(Request.QueryString["workorderid"].ToString());

                if (_workorderid > 0)
                {
                    RSUserIdentity userIdentity = GetRSUserIdentity();
                    _companyid = userIdentity.RSCompanyId;
                    _locationid = RSSBMCommonFunctions.GetLocationIDByWorkOrderID(_companyid, _workorderid);
                    ucLocationHeaderControl.LoadLocationHeader(_companyid, _locationid);
                    ucWorkOrderEquipListControl.LoadWorkOrderEquipListByID(_companyid, _workorderid);

                    lblWorkOrderEquip.Text = "WorkOrder ID: " + _workorderid.ToString();
                }
            }
        }

        protected void btnCheckoutEquip_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
               if (Request.QueryString["workorderid"] != null)
                    _workorderid = Convert.ToInt32(Request.QueryString["workorderid"].ToString());

               if (_workorderid > 0)
               {
                   Response.Redirect("~/RS/SBM/RSCheckoutEquip.aspx?action=add&workorderid=" + _workorderid);
               }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private void ActiveTabChanged(int workorderid)
        {
            if (workorderid > 0)
            {
                switch (tcWorkOrderDetail.ActiveTabIndex)
                {
                    case 0:
                        Response.Redirect("RSWorkOrderDetail.aspx?workorderid=" + workorderid);
                        break;
                    case 1:
                        Response.Redirect("RSWorkOrderTasks.aspx?workorderid=" + workorderid);
                        break;
                    case 2:
                        Response.Redirect("RSWorkOrderParts.aspx?workorderid=" + workorderid);
                        break;
                    case 3:
                        //Response.Redirect("RSWorkOrderEquips.aspx?workorderid=" + workorderid);
                        break;
                    case 4:
                        Response.Redirect("RSWorkOrderIssues.aspx?workorderid=" + workorderid);
                        break;
                    case 5:
                        Response.Redirect("RSWorkOrderInvoices.aspx?workorderid=" + workorderid);
                        break;

                    //default:
                }
            }
        }

        protected void tcWorkOrderDetail_OnActiveTabChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["workorderid"]))
                _workorderid = Convert.ToInt32(Request.QueryString["workorderid"].ToString());

            ActiveTabChanged(_workorderid);
        }
    }
}